package com.sicnu.cheer.generalmodule.adapter.viewholder;

import android.content.Context;
import android.graphics.Bitmap;  
import android.util.SparseArray;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.ImageView;  
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;

public class CommonViewHolder
{
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    private CommonViewHolder(Context context, ViewGroup parent, int layoutId,
                             int position)
    {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        // setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static CommonViewHolder get(Context context, View convertView,
                                       ViewGroup parent, int layoutId, int position)
    {
        if (convertView == null)
        {
            return new CommonViewHolder(context, parent, layoutId, position);
        }
        return (CommonViewHolder) convertView.getTag();
    }

    public View getConvertView()
    {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId)
    {
        View view = mViews.get(viewId);
        if (view == null)
        {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public CommonViewHolder setText(int viewId, String text)
    {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public CommonViewHolder setImageResource(int viewId, int drawableId)
    {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }
    /**
     *  为ImageView设置图片
     * @param viewId
     * @param bm
     * @return
     */
    public CommonViewHolder setImageBitmap(int viewId, Bitmap bm)
    {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }
    /**
     * 为ImageView设置图片
     * @param viewId
     * @param url
     * @return
     */
    public CommonViewHolder setImageByUrl(int viewId, String url)
    {
        Glide.with(mConvertView.getContext()).load(url).into((ImageView) getView(viewId));
//        ImageLoader.getInstance(3, Type.LIFO).loadImage(url,
//                (ImageView) getView(viewId));
        return this;
    }
    /**
     * 为ImageView设置图片
     * @param viewId
     * @param url
     * @return
     */
    public CommonViewHolder setImageByUrl(int viewId, String url,int defaltRes)
    {
        Glide.with(mConvertView.getContext()).load(url).placeholder(defaltRes).into((ImageView) getView(viewId));
//        ImageLoader.getInstance(3, Type.LIFO).loadImage(url,
//                (ImageView) getView(viewId));
        return this;
    }
    /**
     * 为View设置显示隐藏状态
     * @param viewId
     * @param visible
     * @return
     */
    public CommonViewHolder setMyVisibility(int viewId, boolean visible)
    {
        View view = getView(viewId);
        view.setVisibility(visible?View.VISIBLE:View.GONE);
        return this;
    }
    /**
     * 为View设置选中状态
     * @param viewId
     * @param select
     * @return
     */
    public CommonViewHolder setSelectState(int viewId, boolean select)
    {
        View view = getView(viewId);
        view.setSelected(select);
        return this;
    }
    /**
     * 为View设点击事件
     * @param viewId
     * @param listener
     * @return
     */
    public CommonViewHolder setOnclick(int viewId, View.OnClickListener listener)
    {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }
    /**
     * 为ImageView设置图片
     * @param viewId
     * @param url
     * @return
     */
    public CommonViewHolder setCircleImageByUrl(int viewId, String url)
    {
        Glide.with(mConvertView.getContext()).load(url).into((ImageView) getView(viewId));
        return this;
    }
  
    public int getPosition()  
    {  
        return mPosition;  
    }  
  
}  